#include "widget.h"
#include "ui_widget.h"
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    initUI();
    connectSignalAndSlot();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initUI()
{
     setWindowFlags(Qt::FramelessWindowHint);
    // setAttribute(Qt::WA_TranslucentBackground);

    QGraphicsDropShadowEffect* dropShadow = new QGraphicsDropShadowEffect(this);
    dropShadow->setColor(Qt::black);
    dropShadow->setBlurRadius(5);
    dropShadow->setOffset(0, 0);
    ui->bitPlayerBg->setGraphicsEffect(dropShadow);

    ui->homePageBtn->setTextColor("#000000");
    ui->homePageBtn->setImageAndText(":/images/homePage/shouyexuan.png", "首页", HomePage);
    ui->myPageBtn->setTextColor("#999999");
    ui->myPageBtn->setImageAndText(":/images/homePage/wode.png", "我的", MyselfPage);
    ui->sysPageBtn->setTextColor("#999999");
    ui->sysPageBtn->setImageAndText(":/images/homePage/admin.png", "系统", AdminPage);

    ui->stackedWidget->setCurrentIndex(0); //默认情况下选中首页

    setWindowIcon(QIcon(":/images/homePage/logo.png"));
}


void Widget::mousePressEvent(QMouseEvent *event)
{
    // 先要获取鼠标点击时的位置
    QPoint point = event->position().toPoint();

    // 检测该位置是否在head中 且 是鼠标左键单击时
    if(ui->head->geometry().contains(point)){
        if(event->button() == Qt::LeftButton){
            dragPos = event->globalPosition().toPoint() - geometry().topLeft();
            return;
        }
    }

    QWidget::mousePressEvent(event);
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint point = event->position().toPoint();
    if(ui->head->geometry().contains(point)){
        if(event->buttons() == Qt::LeftButton){
            move(event->globalPosition().toPoint() - dragPos);
            return;
        }
    }
}

void Widget::connectSignalAndSlot()
{
    connect(ui->minBtn, &QPushButton::clicked, this, &QWidget::showMinimized);
    connect(ui->quitBtn, &QPushButton::clicked, this, [=]{
        close();
    });

    connect(ui->homePageBtn, &PageSwitchButton::switchPage, this, &Widget::onSwitchPage);
    connect(ui->myPageBtn, &PageSwitchButton::switchPage, this, &Widget::onSwitchPage);
    connect(ui->sysPageBtn, &PageSwitchButton::switchPage, this, &Widget::onSwitchPage);


}

void Widget::onSwitchPage(int pageId)
{
    onSwitchUI(pageId);

    //如果pageId
}

void Widget::onSwitchUI(int pageId)
{
    ui->stackedWidget->setCurrentIndex(pageId);
    resetswitchBtnInfo(pageId);
}

void Widget::resetswitchBtnInfo(int pageId)
{
    QList<PageSwitchButton*> switchBtns = findChildren<PageSwitchButton*>();
    for(auto& switchBtn: switchBtns)
    {
        if(switchBtn->getPageId() != pageId)
        {
            switchBtn->setTextColor("#999999");
        }
    }

    // 设置按钮的图片
    if(pageId == HomePage){
        // 在HomePageBtn的图片设置为高亮
        // 将MyselfPageBtn、AdminPageBtn的图片设置为灰色
        ui->homePageBtn->setImage(":/images/homePage/shouyexuan.png");
        ui->myPageBtn->setImage(":/images/homePage/wode.png");
        ui->sysPageBtn->setImage(":/images/homePage/admin");
    }else if(pageId == MyselfPage){
        ui->homePageBtn->setImage(":/images/homePage/shouye.png");
        ui->myPageBtn->setImage(":/images/homePage/wodexuan.png");
        ui->sysPageBtn->setImage(":/images/homePage/admin");
    }else if(pageId == AdminPage){
        ui->homePageBtn->setImage(":/images/homePage/shouye.png");
        ui->myPageBtn->setImage(":/images/homePage/wode.png");
        ui->sysPageBtn->setImage(":/images/homePage/adminxuan");
    }else{
       // LOG()<<"暂不支持的页面";
    }

}






